#include <iostream>
#include <memory>

class A : public std::enable_shared_from_this<A> {
 private:
  /* data */
 public:
  A(/* args */) {}
  ~A() {}

 public:
  std::shared_ptr<A> sharedPtr() {
    // 这里会生成一个新的shared指针,原始指针相同，自动释放时会释放两次
    // return std::shared_ptr<A>(this);
    return shared_from_this();
  }

  void Display() { std::cout << "Display\n"; }
};

int main(int argc, char const* argv[]) {
  std::shared_ptr<A> a(new A);
  {
    a->Display();
    auto a1 = a->sharedPtr();
    a1->Display();
    std::cout << "--->" << a.use_count() << "\n";
  }
  std::cout << "a--->" << a.use_count() << "\n";
  a->Display();
  return 0;
}
